<html lang="en">
	<head>
		<title>Yuka | Steering Behaviors | Follow Path</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link rel="stylesheet" type="text/css" href="../../lib/styles.css">
		<link rel="shortcut icon" type="image/x-icon" href="https://mugen87.github.io/yuka/favicon.ico">
	</head>
<body>

	<section id="info">
		<p>
			This steering behavior produces a force that moves a vehicle along a series of waypoints forming a path.<br/>
			An additional steering behavior (OnPath) can be used to realize a more strict path following.
		</p>
	</section>

	<script type="module">

		import * as YUKA from '../../../build/yuka.module.js';
		import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.109/build/three.module.js';
		import * as DAT from 'https://cdn.jsdelivr.net/npm/dat.gui@0.7.7/build/dat.gui.module.js';

		let renderer, scene, camera;

		let entityManager, time, vehicle;

		let onPathBehavior;

		const params = {
			onPathActive: true,
			radius: 0.1
		};

		init();
		animate();

		function init() {

			scene = new THREE.Scene();

			camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
			camera.position.set( 0, 20, 0 );
			camera.lookAt( scene.position );

			//

			const vehicleGeometry = new THREE.ConeBufferGeometry( 0.1, 0.5, 8 );
			vehicleGeometry.rotateX( Math.PI * 0.5 );
			const vehicleMaterial = new THREE.MeshNormalMaterial();

			const vehicleMesh = new THREE.Mesh( vehicleGeometry, vehicleMaterial );
			vehicleMesh.matrixAutoUpdate = false;
			scene.add( vehicleMesh );

			// dat.gui

			const gui = new DAT.GUI( { width: 300 } );

			gui.add( params, 'onPathActive' ).name( 'activate onPath' ).onChange( ( value ) => onPathBehavior.active = value );
			gui.add( params, 'radius', 0.01, 1 ).name( 'radius' ).onChange( ( value ) => onPathBehavior.radius = value );

			gui.open();

			//

			renderer = new THREE.WebGLRenderer( { antialias: true } );
			renderer.setPixelRatio( window.devicePixelRatio );
			renderer.setSize( window.innerWidth, window.innerHeight );
			document.body.appendChild( renderer.domElement );

			//

			window.addEventListener( 'resize', onWindowResize, false );

			// game setup

			entityManager = new YUKA.EntityManager();
			time = new YUKA.Time();
			vehicle = new YUKA.Vehicle();

			vehicle.setRenderComponent( vehicleMesh, sync );

			const path = new YUKA.Path();
			path.loop = true;
			path.add( new YUKA.Vector3( - 4, 0, 4 ) );
			path.add( new YUKA.Vector3( - 6, 0, 0 ) );
			path.add( new YUKA.Vector3( - 4, 0, - 4 ) );
			path.add( new YUKA.Vector3( 0, 0, 0 ) );
			path.add( new YUKA.Vector3( 4, 0, - 4 ) );
			path.add( new YUKA.Vector3( 6, 0, 0 ) );
			path.add( new YUKA.Vector3( 4, 0, 4 ) );
			path.add( new YUKA.Vector3( 0, 0, 6 ) );

			vehicle.position.copy( path.current() );

			// use "FollowPathBehavior" for basic path following

			const followPathBehavior = new YUKA.FollowPathBehavior( path, 0.5 );
			vehicle.steering.add( followPathBehavior );

			// use "OnPathBehavior" to realize a more strict path following.
			// it's a separate steering behavior to provide more flexibility.

			onPathBehavior = new YUKA.OnPathBehavior( path );
			vehicle.steering.add( onPathBehavior );

			entityManager.add( vehicle );

			//

			const position = [];

			for ( let i = 0; i < path._waypoints.length; i ++ ) {

				const waypoint = path._waypoints[ i ];

				position.push( waypoint.x, waypoint.y, waypoint.z );

			}

			const lineGeometry = new THREE.BufferGeometry();
			lineGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );

			const lineMaterial = new THREE.LineBasicMaterial( { color: 0xffffff } );
			const lines = new THREE.LineLoop( lineGeometry, lineMaterial );
			scene.add( lines );

		}

		function onWindowResize() {

			camera.aspect = window.innerWidth / window.innerHeight;
			camera.updateProjectionMatrix();

			renderer.setSize( window.innerWidth, window.innerHeight );

		}

		function animate() {

			requestAnimationFrame( animate );

			const delta = time.update().getDelta();

			entityManager.update( delta );

			renderer.render( scene, camera );

		}

		function sync( entity, renderComponent ) {

			renderComponent.matrix.copy( entity.worldMatrix );

		}

	</script>

</body>
</html>
